class Solution {
public:
    vector<vector<int>> threeSum(vector<int>& nums) {

        sort(nums.begin(), nums.end());

        vector<vector<int>> res;

        for(int i = 0; i < nums.size(); i++)
        {
            if(nums[i] > 0) break;
            if(i > 0 && nums[i] == nums[i - 1])continue; // 去重

            int j = i + 1, k = nums.size() - 1;

            while(j < k)
            {
                int sum = nums[i] + nums[j] + nums[k];

                if(sum > 0)
                {
                    k--;
                }
                else if(sum < 0)
                {
                    j++;
                }
                else 
                {
                    res.push_back({nums[i], nums[j], nums[k]});
                    j++, k--;
                    while(j < k && nums[j] == nums[j - 1])j++;   //去重
                    while(j < k && nums[k] == nums[k + 1])k--;   //去重
                }

            }
            
        }

        return res;     
    }
};

// Find three numbers a,b,c in the array and satisfy the condition a+b+c = 0
// Attention : the answer should not contain duplicate ternary array